const Column = require('../../../models/blog_column');

/**
 * 栏目管理控制器
 */
const ControllerColumn = {
    /* 获取列表 */
    getList: async function (req, res, next) {
        try {
            const list = await Column.all();

            res.json({ code: 200, msg: '成功连接', data: list })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 获取详情 */
    getById: async function (req, res, next) {
        try {
            let { id } = req.query;

            const columns = await Column.select({ id });

            res.json({ code: 200, msg: '成功连接', data: columns[0] })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 添加 */
    Insert: async function (req, res, next) {
        try {
            let params = {
                pid: req.body.pid ? Number(req.body.pid) : 0,
                name: req.body.name ? String(req.body.name) : '',
                title: req.body.title ? String(req.body.title) : '',
                path: req.body.path ? String(req.body.path) : '',
                keyword: req.body.keyword ? String(req.body.keyword) : '',
                desc: req.body.desc ? String(req.body.desc) : '',
                display: req.body.display ? Number(req.body.display) : 1,
                sort: req.body.sort ? Number(req.body.sort) : 0,
                created_time: new Date().getTime()
            }

            if (!params.name) return res.json({ code: 0, msg: '请输入栏目名称' });

            if (!params.path) return res.json({ code: 0, msg: '请输入路径' });

            await Column.insert(params);

            res.json({ code: 200, msg: '添加成功', data: {} })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 编辑 */
    update: async function (req, res, next) {
        try {
            let { id, pid, title, name, path, keyword, desc, display, sort } = req.body;
            let params = {}
            if (display == 0 || display == 1) {
                params.display = display;
            } else {
                params = {
                    pid: pid ? Number(pid) : 0,
                    name: name ? String(name) : '',
                    title: title ? String(title) : '',
                    path: path ? String(path) : '',
                    keyword: keyword ? String(keyword) : '',
                    desc: desc ? String(desc) : '',
                    display: display ? Number(display) : 1,
                    sort: sort ? Number(sort) : 0,
                    update_time: new Date().getTime()
                };

                if (!name) return res.json({ code: 0, msg: '请输入栏目名称' });

                if (!path) return res.json({ code: 0, msg: '请输入路径' });
            }

            await Column.update(id, params)

            res.json({ code: 200, msg: '修改成功', data: {} })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 删除 */
    del: async function (req, res, next) {
        try {
            let { id } = req.body;

            await Column.delete(id)

            res.json({ code: 200, msg: '成功连接', data: {} })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 排序 */
    onUpdateSort: async function (req, res, next) {
        try {
            let sortList = JSON.parse(req.body.sortList);

            sortList.forEach(async data => {
                let id = data.id;

                await Column.update(id, { sort: data.sort })
            })
            res.json({ code: 200, msg: '成功连接', data: {} })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    }
}

module.exports = ControllerColumn;